package com.yanqu.road.dao.impl.channelcenter.washe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheBattleRecord;
import com.yanqu.road.entity.activity.washe.data.WasheBattleRecordOpponent;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WasheBattleRecordDaoImpl extends TempDataObjectDao<WasheBattleRecord> {

    public static String getWasheBattleRecordSql = "select * from `t_u_washe_battle_record` where `userId`=? and `type`=? order by `no` desc limit ?"; // 最后要反过来，不然倒序了

    public List<WasheBattleRecord> getWasheBattleRecordList(long userId, int type, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<WasheBattleRecord> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWasheBattleRecordSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, type);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheBattleRecord record = parse(rs);
                list.add(record);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        // 【sql是反着的，这里再反回去】
        Collections.reverse(list);
        return list;
    }

    @Override
    protected boolean updateData(WasheBattleRecord washeBattleRecord) {
        return false;
    }

    public static String insertWasheBattleRecordSql = "insert into `t_u_washe_battle_record`(userId,no,time,opponentInfo,opponentCardId,opponentScore,selfCardId,selfScore,win,scoreChange,type,selfSkills,opponentSkills,jinBiaoScoreChange) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(WasheBattleRecord washeBattleRecord) {
        washeBattleRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getNo()));
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeBattleRecord.getOpponentInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, washeBattleRecord.getOpponentCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getOpponentScore()));
        para.put(i++, new DbParameter(Types.INTEGER, washeBattleRecord.getSelfCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getSelfScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeBattleRecord.isWin()));
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getScoreChange()));
        para.put(i++, new DbParameter(Types.INTEGER, washeBattleRecord.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeBattleRecord.getSelfSkills())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeBattleRecord.getOpponentSkills())));
        para.put(i++, new DbParameter(Types.BIGINT, washeBattleRecord.getJinBiaoScoreChange()));
        if (getDb().executeNoneQuery(insertWasheBattleRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WasheBattleRecord parseData(ResultSet rs) throws SQLException {
        WasheBattleRecord washeBattleRecord = new WasheBattleRecord();
        washeBattleRecord.setUserId(rs.getLong("userId"));
        washeBattleRecord.setNo(rs.getLong("no"));
        washeBattleRecord.setTime(rs.getLong("time"));
        washeBattleRecord.setOpponentInfo(JSON.parseObject(rs.getString("opponentInfo"), new TypeReference<WasheBattleRecordOpponent>() {
        }));
        washeBattleRecord.setOpponentCardId(rs.getInt("opponentCardId"));
        washeBattleRecord.setOpponentScore(rs.getLong("opponentScore"));
        washeBattleRecord.setSelfCardId(rs.getInt("selfCardId"));
        washeBattleRecord.setSelfScore(rs.getLong("selfScore"));
        washeBattleRecord.setWin(rs.getBoolean("win"));
        washeBattleRecord.setScoreChange(rs.getLong("scoreChange"));
        washeBattleRecord.setType(rs.getInt("type"));

        String selfSkills = rs.getString("selfSkills");
        if (StringUtils.isNullOrEmpty(selfSkills)) {
            selfSkills = "[1001,1002]";
        }
        washeBattleRecord.setSelfSkills(JSON.parseObject(selfSkills, new TypeReference<List<Integer>>() {
        }));

        String opponentSkills = rs.getString("opponentSkills");
        if (StringUtils.isNullOrEmpty(opponentSkills)) {
            opponentSkills = "[1001,1002]";
        }
        washeBattleRecord.setOpponentSkills(JSON.parseObject(opponentSkills, new TypeReference<List<Integer>>() {
        }));

        washeBattleRecord.setJinBiaoScoreChange(rs.getLong("jinBiaoScoreChange"));

        return washeBattleRecord;
    }

}
